//
//  MainView.swift
//  jiweiapp
//
//  Created by Kin Wong on 15/8/13.
//  Copyright (c) 2015年 Kin Wong. All rights reserved.
//

import UIKit

class MainView: UIViewController, UITableViewDelegate, UITableViewDataSource {
    
    var tableview: UITableView? //创建列表
    var titles = [String]() //创建列表数据数组
    var details = [String]()
    var xmlData = XMLAnalyser()
    
    override func loadView() {
        super.loadView()
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        (titles, details) = xmlData.readOLXML("Messages")
        self.view.backgroundColor = UIColor.whiteColor() //设置背景颜色
        self.tableview = UITableView(frame: CGRectMake(0, 0, CGRectGetWidth(self.view.bounds), CGRectGetHeight(self.view.bounds)-60), style: UITableViewStyle.Plain) //实例化列表
        self.tableview!.delegate = self
        self.tableview!.dataSource = self //设置数据来源
        self.tableview!.registerClass(UITableViewCell.self, forCellReuseIdentifier: "MessageCell") //创建列表单元
        self.tableview!.tableFooterView = UIView(frame: CGRectZero) //隐藏多余的分割线
        self.view.addSubview(self.tableview!) //显示部件
        // Do any additional setup after loading the view.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    func numberOfSection(tableView: UITableView!) -> Int {
        return 1
    } //返回列表分类数目
    
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.titles.count
    } //返回列表数目
    
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let identify: String = "MessageCell"
        
        let cell = UITableViewCell(style: UITableViewCellStyle.Subtitle, reuseIdentifier: identify)
        cell.accessoryType = UITableViewCellAccessoryType.DisclosureIndicator
        cell.textLabel?.text = self.titles[indexPath.row]
        cell.detailTextLabel?.text = self.details[indexPath.row]
        
        return cell
    } //创建可重用列表单元
    
    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        self.tableview!.deselectRowAtIndexPath(indexPath, animated: true)
        
        var itemString = self.titles[indexPath.row]
        var itemDetail = self.details[indexPath.row]
        
        var detail = DetailsView()
        detail.itemString = itemString
        detail.itemDetail = itemDetail
        detail.title = itemString
        self.navigationController?.pushViewController(detail, animated: true)
        
    } //监听列表操作
    
    func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
        if editingStyle == UITableViewCellEditingStyle.Delete {
            titles.removeAtIndex(indexPath.row)
            tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Automatic)
        }
    }
    
    func tableView(tableView: UITableView, titleForDeleteConfirmationButtonForRowAtIndexPath indexPath: NSIndexPath) -> String! {
        return "清除"
    }
    
}
